package com.frank.activedatabus.dispatch;

import com.frank.activedatabus.Core.LiveEventBusCore;
import ohos.eventhandler.EventHandler;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @version 1.0.1
 * @description: 后台线程分发器
 * @program: Gallery
 * @Author xiaozhijun
 * @Date 2020/12/24 10:12
 */
public class BackgroudDispatch implements IDispatch {

    private EventHandler bgHandler;

    public BackgroudDispatch(EventHandler bg) {
        this.bgHandler = bg;
    }

    @Override
    public synchronized void post(Runnable task) {
        //先判断是否有线程池工具
        ThreadPoolExecutor executor = LiveEventBusCore.get().config().getThreadPoolExecutor();
        if (executor != null) {
            executor.execute(task);
        } else {
            if (bgHandler != null) {
                bgHandler.postSyncTask(task);
            }
        }
    }
}
